// nodemon .\index.js
const express = require('express')
const path = require('path')
const vertoken=require('./token/token')
const expressJwt=require('express-jwt')
const testRouter = require('./routers/media')
// const goodsRouter = require('./routers/goodsRouter')
// const userRouter = require('./routers/userRouter')
// const roleRouter = require('./routers/roleRouter')
// const brandsRouter = require('./routers/brandsRouter')
// const permissionRouter = require('./routers/permissionRouter')
// const categoryRouter = require('./routers/categoryRouter')
// const consigneeRouter = require('./routers/consigneeRouter')
// const orderRouter = require('./routers/orderRouter')
// const expressRouter = require('./routers/expressRouter')
// const userCartRouter = require('./routers/userCartRouter')
// const uploadRouter = require('./routers/uploadRouter')
// const arrtributeRouter = require('./routers/arrtributeRouter')
// const abcRouter = require('./routers/abcRouter')
const app = express()

const cors = require('cors')
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({extends:false}))
const port = 3000
// 注册路由，app


//解析token获取用户信息
app.use(function(req, res, next) {
    var token = req.headers['authorization'];
    if(token == undefined){
        return next();
    }else{
        vertoken.getToken(token).then((data)=> {
            req.data = data;
            console.log(data)
            console.log(12345878)
            console.log('token验证成功')
            return next();
        }).catch((error)=>{
            console.log(error)
            return next();
        })
    }
});

//验证token是否过期并规定那些路由不需要验证
app.use(expressJwt({
    secret:'zgs_first_token',
    algorithms:['HS256']

}).unless({
    path:['/test','/loginCustomer']  //不需要验证的接口名称
}))

//设置托管静态目录; 项目根目录+ public.可直接访问public文件下的文件eg:http://localhost:3000/images/url.jpg
// app.use('/', indexRouter);
// app.use('/', usersRouter);



//token失效返回信息
app.use(function(err,req,res,next){
    if(err.status==401){
        return res.status(401).send('token失效')
         //可以设置返回json 形式  res.json({message:'token失效'})
    }
})

app.use(testRouter)
// app.use(goodsRouter)
// app.use(userRouter)
// app.use(permissionRouter)
// app.use(roleRouter)
// app.use(brandsRouter)
// app.use(categoryRouter)
// app.use(consigneeRouter)
// app.use(orderRouter)
// app.use(expressRouter)
// app.use(userCartRouter)
// app.use(arrtributeRouter)
// app.use(uploadRouter)
// 路由增加前缀
app.listen(port, () => console.log(`SCCTServer app listening on port ${port}!`))